﻿using Hello.Api.Contracts;
using Hello.Api.Filters;
using MassTransit;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Hello.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class HelloController : ControllerBase
    {
        private readonly IPublishEndpoint publishEndpoint;
        private readonly ISendEndpointProvider sendEndpointProvider;
        private readonly Tenant tenant;
        private readonly IRequestClient<MyRequest> requestClient;

        public HelloController(IPublishEndpoint endpoint,
            ISendEndpointProvider sendEndpointProvider,
            Tenant tenant,
            IRequestClient<MyRequest> requestClient) {
            publishEndpoint = endpoint;
            this.sendEndpointProvider= sendEndpointProvider;
            this.tenant= tenant;
            this.tenant.MyValue = "MyCoolTenant";
            this.requestClient= requestClient;
        }

        [HttpGet]
        public async Task<IActionResult> Get()
        {
            var messageToSend = new Message() { Text="Hello from api" };
            //await publishEndpoint.Publish(messageToSend);

            await publishEndpoint.Publish(new Email
            {
                Destination = "",
                Subject="Hello from an email"
            });
            //await publishEndpoint.Publish<Message>(messageToSend, publishContext =>
            //{
            //    publishContext.Headers.Set("Publish-Context", "en-us");
            //    publishContext.SetRoutingKey("my-direct-routing-key");
            //});
            var sendEndpoint = await sendEndpointProvider.GetSendEndpoint(new Uri("queue:hello.queue"));
            ////await sendEndpoint.Send(messageToSend);
            await sendEndpoint.Send<Message>(new
            {
                messageToSend.Text
            },
            sendContext =>
            {
                sendContext.Headers.Set("Culture", "en-us");
            }
            );
            return Ok("Hello World");
        }

        [HttpGet("/request")]
        public async Task<IActionResult> Request()
        {
            var myResponse=await requestClient.GetResponse<MyResponse>(new MyRequest
            {
                Id=Guid.NewGuid(),
                RequestBody="My request body"
            });

            return Ok(myResponse.Message);
        }
    }
}
